<template>
  <el-dialog
    @close="tjkcxxCancel('tjkcxxRuleForm')"
    :close-on-click-modal="false"
    title="提交勘场报告"
    :visible.sync="tjkcxxVisible"
    width="600px"
  >
    <div class="scroll-view">
      <el-form
        :model="tjkcxxRuleForm"
        :rules="tjkcxxRules"
        ref="tjkcxxRuleForm"
        label-position="top"
      >
        <el-form-item label="现场图片 (.jpg,.png,.gif)" prop="scene_file">
          <el-input
            style="position: absolute; z-index: 0; top: -999999px"
            v-model="tjkcxxRuleForm.scene_file.length"
          ></el-input>
          <div class="file-box">
            <el-upload
              :show-file-list="false"
              :auto-upload="false"
              action="#"
              class="avatar-uploader"
              accept="image/*"
              multiple
              :file-list="tjkcxxRuleForm.scene_file"
              :on-change="
                (file, fileList) => {
                  tjkcxxFileChange(file, fileList, 'scene_file');
                }
              "
            >
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div
              class="img-box"
              v-for="(item, index) in tjkcxxRuleForm.scene_file"
              :key="index"
            >
              <div class="operation-box">
                <i
                  class="iconfont icon-shanchu"
                  @click.stop="tjkcxxFileRemove(index, 'scene_file')"
                ></i>
              </div>
              <img class="img-item" :src="item.show" />
            </div>
          </div>
        </el-form-item>
        <el-form-item
          label="勘察报告 (.jpg,.png,.gif,.xlsx,.xls)"
          prop="report_file"
        >
          <el-input
            style="position: absolute; z-index: 0; top: -999999px"
            v-model="tjkcxxRuleForm.report_file.length"
          ></el-input>
          <div class="file-box">
            <el-upload
              :show-file-list="true"
              :auto-upload="false"
              action="#"
              accept="image/*,.xlsx,.xls"
              multiple
              :file-list="tjkcxxRuleForm.report_file"
              :on-change="
                (file, fileList) => {
                  tjkcxxFileChange(file, fileList, 'report_file');
                }
              "
              :on-remove="
                (file, fileList) => {
                  tjkcxxFileRemove(fileList, 'report_file');
                }
              "
            >
              <!-- <i class="el-icon-plus avatar-uploader-icon"></i> -->
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
            <!-- <div
              class="img-box"
              v-for="(item, index) in tjkcxxRuleForm.report_file"
              :key="index"
            >
              <div class="operation-box">
                <i
                  class="iconfont icon-shanchu"
                  @click.stop="tjkcxxFileRemove(index, 'report_file')"
                ></i>
              </div>
              <img class="img-item" :src="item.show" />
            </div> -->
          </div>
        </el-form-item>
        <el-form-item label="视频 (.mp4,.3GP,.WMV)" prop="video_file">
          <el-input
            style="position: absolute; z-index: 0; top: -999999px"
            v-model="tjkcxxRuleForm.video_file.length"
          ></el-input>
          <div class="file-box">
            <el-upload
              :show-file-list="false"
              :auto-upload="false"
              action="#"
              class="avatar-uploader"
              accept="video/*"
              multiple
              :file-list="tjkcxxRuleForm.video_file"
              :on-change="
                (file, fileList) => {
                  tjkcxxFileChange(file, fileList, 'video_file');
                }
              "
            >
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div
              class="img-box"
              v-for="(item, index) in tjkcxxRuleForm.video_file"
              :key="index"
            >
              <div class="operation-box">
                <i
                  class="iconfont icon-shanchu"
                  @click.stop="tjkcxxFileRemove(index, 'video_file')"
                ></i>
              </div>

              <i class="el-icon-video-play"></i>
              <video class="img-item" :src="item.show"></video>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="手绘图 (.jpg,.png,.gif)" prop="sketch_file">
          <el-input
            style="position: absolute; z-index: 0; top: -999999px"
            v-model="tjkcxxRuleForm.sketch_file.length"
          ></el-input>
          <div class="file-box">
            <el-upload
              :show-file-list="false"
              :auto-upload="false"
              action="#"
              class="avatar-uploader"
              accept="image/*"
              multiple
              :file-list="tjkcxxRuleForm.sketch_file"
              :on-change="
                (file, fileList) => {
                  tjkcxxFileChange(file, fileList, 'sketch_file');
                }
              "
            >
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div
              class="img-box"
              v-for="(item, index) in tjkcxxRuleForm.sketch_file"
              :key="index"
            >
              <div class="operation-box">
                <i
                  class="iconfont icon-shanchu"
                  @click.stop="tjkcxxFileRemove(index, 'sketch_file')"
                ></i>
              </div>
              <img class="img-item" :src="item.show" />
            </div>
          </div>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            :rows="4"
            class="input-box"
            v-model="tjkcxxRuleForm.remark"
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer">
      <el-button
        type="primary"
        @click="tjkcxxSubmit('tjkcxxRuleForm')"
        :disabled="diaLoading"
        :loading="diaLoading"
        >提交</el-button
      >
      <el-button @click="tjkcxxCancel('tjkcxxRuleForm')">关闭</el-button>
    </span>
  </el-dialog>
</template>
<script>
import projectApi from "@/api/projectApi";
import companyApi from "@/api/companyApi";
import adminApi from "@/api/adminApi";
import { getUserInfo } from "@/utils/auth";
import foremanApi from "@/api/foremanApi";

export default {
  name: "",
  props: {
    addDialogVisible: {
      type: Boolean,
      default: false,
      required: false,
    },
    project_id: {
      required: false,
    },
  },
  data() {
    var file1 = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error("请上传现场图片"));
      } else {
        callback();
      }
    };
    var file2 = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error("请上传勘察报告"));
      } else {
        callback();
      }
    };
    var file3 = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error("请上传视频"));
      } else {
        callback();
      }
    };
    var file4 = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error("请上传手绘图"));
      } else {
        callback();
      }
    };
    return {
      tjkcxxRuleForm: {
        scene_file: [],
        report_file: [],
        video_file: [],
        sketch_file: [],
        remark: "",
      },
      tjkcxxVisible: false,
      tjkcxxRules: {
        scene_file: {
          required: true,
          validator: file1,
          trigger: "change",
        },
        report_file: {
          required: true,
          validator: file2,
          trigger: "change",
        },
        video_file: {
          required: true,
          validator: file3,
          trigger: "change",
        },
        sketch_file: {
          required: true,
          validator: file4,
          trigger: "change",
        },
      },
      diaLoading: false,
    };
  },
  watch: {
    addDialogVisible(val1, val2) {
      this.tjkcxxVisible = this.addDialogVisible;
    },
  },
  computed: {},
  mounted() {},
  methods: {
    // 关闭提交勘场报告弹窗关闭
    tjkcxxCancel(formName) {
      this.tjkcxxRuleForm = {
        scene_file: [],
        report_file: [],
        video_file: [],
        sketch_file: [],
        remark: "",
      };
      this.$nextTick(() => {
        this.$refs[formName].clearValidate();
        this.$emit("onClose", true);
      });
    },
    // 提交勘场报告
    tjkcxxSubmit(formName) {
      let tjkcxxRuleForm = this.tjkcxxRuleForm;
      console.log(tjkcxxRuleForm, "tjkcxxRuleForm");
      // if (
      //   !tjkcxxRuleForm.scene_file.length &&
      //   !tjkcxxRuleForm.report_file.length &&
      //   !tjkcxxRuleForm.video_file.length &&
      //   !tjkcxxRuleForm.sketch_file.length &&
      //   !tjkcxxRuleForm.remark
      // ) {
      //   this.$message.warning("请填写勘场报告内容");
      //   return;
      // }

      this.$refs[formName].validate((valid) => {
        if (valid) {
          let formdata = new FormData();
          tjkcxxRuleForm.scene_file.forEach((item) => {
            formdata.append("scene_file[]", item.raw);
          });
          tjkcxxRuleForm.report_file.forEach((item) => {
            formdata.append("report_file[]", item.raw);
          });
          tjkcxxRuleForm.video_file.forEach((item) => {
            formdata.append("video_file[]", item.raw);
          });
          tjkcxxRuleForm.sketch_file.forEach((item) => {
            formdata.append("sketch_file[]", item.raw);
          });
          formdata.append(
            "remark",
            JSON.stringify({
              remark: tjkcxxRuleForm.remark,
              project_id: this.project_id,
            })
          );
          this.diaLoading = true;
          projectApi
            .submitSurveyInformation(formdata)
            .then((res) => {
              console.log(res, "提交勘场报告");
              this.diaLoading = false;
              if (res.code == 200) {
                this.$emit("onSuccess", true);
              }
              this.$message({
                type: res.code == 200 ? "success" : "warning",
                message: res.code == 200 ? "处理成功" : res.message,
              });
            })
            .catch((err) => {
              this.diaLoading = false;
              this.$message({
                type: "warning",
                message: err.message,
              });
            });
        } else {
          return false;
        }
      });
    },
    // 提交勘场信息文件选择
    tjkcxxFileChange(file, fileList, type) {
      console.log(fileList, type);
      let tjkcxxRuleForm = this.tjkcxxRuleForm;
      fileList.forEach((item) => {
        item.show = URL.createObjectURL(item.raw);
      });
      switch (type) {
        // 现场图片
        case "scene_file":
          tjkcxxRuleForm.scene_file = fileList;
          break;
        // 勘察报告
        case "report_file":
          tjkcxxRuleForm.report_file = fileList;
          break;
        // 视频
        case "video_file":
          tjkcxxRuleForm.video_file = fileList;
          break;
        // 手绘图
        case "sketch_file":
          tjkcxxRuleForm.sketch_file = fileList;
          break;
        default:
      }
      this.tjkcxxRuleForm = tjkcxxRuleForm;
    },
    // 提交勘场信息文件选择（删除）
    tjkcxxFileRemove(index, type) {
      console.log(index, type);
      let tjkcxxRuleForm = this.tjkcxxRuleForm;
      switch (type) {
        // 现场图片
        case "scene_file":
          tjkcxxRuleForm.scene_file.splice(index, 1);
          break;
        // 勘察报告
        case "report_file":
          tjkcxxRuleForm.report_file = index; //文件
          break;
        // 视频
        case "video_file":
          tjkcxxRuleForm.video_file.splice(index, 1);
          break;
        // 手绘图
        case "sketch_file":
          tjkcxxRuleForm.sketch_file.splice(index, 1);
          break;
        default:
      }
      this.tjkcxxRuleForm = tjkcxxRuleForm;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "./index.scss";
</style>